package com.edu.exam.entity.examination.paper.technical;

import com.edu.exam.enums.examination.paper.SegmentationQualityLevel;
import com.edu.exam.enums.examination.paper.SegmentationQualityStatus;
import com.edu.exam.enums.examination.paper.SegmentationEvaluationMethod;
import com.edu.exam.enums.examination.paper.SegmentationManualReviewStatus;
import com.edu.exam.enums.examination.paper.SegmentationQualityTrendStatus;

import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.experimental.Accessors;

import java.time.LocalDateTime;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

/**
 * 智能切分质量评估实体类
 *
 * 根据E-04-04 智能切分子模块设计实现
 * 对应exam_segmentation_quality数据表
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Data
@Builder
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
public class SegmentationQuality {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    /**
     * 关联的切分任务ID，关联exam_segmentation_task表
     */
    @Column(name = "task_id", nullable = false)
    private Long taskId;

    /**
     * 质量等级：EXCELLENT（优秀）、GOOD（良好）、FAIR（中等）、POOR（差）、UNACCEPTABLE（不可接受）
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "quality_level", nullable = false, length = 20)
    private SegmentationQualityLevel qualityLevel;

    /**
     * 质量状态：PASSED（通过）、FAILED（失败）、PENDING（待评估）、REVIEWING（审核中）、REJECTED（被拒绝）
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "quality_status", nullable = false, length = 20)
    private SegmentationQualityStatus qualityStatus;

    /**
     * 总体质量分数（0-100）
     */
    @Column(name = "overall_quality_score")
    private Double overallQualityScore;

    /**
     * 边界检测质量分数（0-100）
     */
    @Column(name = "boundary_detection_score")
    private Double boundaryDetectionScore;

    /**
     * 区域完整性分数（0-100）
     */
    @Column(name = "region_completeness_score")
    private Double regionCompletenessScore;

    /**
     * 定位精度分数（0-100）
     */
    @Column(name = "positioning_accuracy_score")
    private Double positioningAccuracyScore;

    /**
     * 内容提取质量分数（0-100）
     */
    @Column(name = "content_extraction_score")
    private Double contentExtractionScore;

    /**
     * 格式一致性分数（0-100）
     */
    @Column(name = "format_consistency_score")
    private Double formatConsistencyScore;

    /**
     * 评估方法：AUTOMATIC（自动评估）、MANUAL（人工评估）、HYBRID（混合评估）
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "evaluation_method", nullable = false, length = 20)
    private SegmentationEvaluationMethod evaluationMethod;

    /**
     * 评估算法名称
     */
    @Column(name = "evaluation_algorithm", length = 100)
    private String evaluationAlgorithm;

    /**
     * 置信度水平（0-100）
     */
    @Column(name = "confidence_level")
    private Double confidenceLevel;

    /**
     * 评估时间
     */
    @Column(name = "evaluation_time", nullable = false)
    private LocalDateTime evaluationTime;

    /**
     * 评估耗时（毫秒）
     */
    @Column(name = "evaluation_duration_ms")
    private Long evaluationDurationMs;

    /**
     * 检测到的缺陷总数
     */
    @Column(name = "total_defects")
    private Integer totalDefects;

    /**
     * 严重缺陷数
     */
    @Column(name = "critical_defects")
    private Integer criticalDefects;

    /**
     * 主要缺陷数
     */
    @Column(name = "major_defects")
    private Integer majorDefects;

    /**
     * 次要缺陷数
     */
    @Column(name = "minor_defects")
    private Integer minorDefects;

    /**
     * 缺陷详情（JSON格式）
     */
    @Column(name = "defect_details", length = 2000, columnDefinition = "TEXT")
    private String defectDetails;

    /**
     * 质量指标详情（JSON格式）
     */
    @Column(name = "quality_metrics", length = 2000, columnDefinition = "TEXT")
    private String qualityMetrics;

    /**
     * 边界检测结果（JSON格式）
     */
    @Column(name = "boundary_analysis_result", length = 2000, columnDefinition = "TEXT")
    private String boundaryAnalysisResult;

    /**
     * 区域覆盖分析结果（JSON格式）
     */
    @Column(name = "region_coverage_analysis", length = 2000, columnDefinition = "TEXT")
    private String regionCoverageAnalysis;

    /**
     * 内容质量分析结果（JSON格式）
     */
    @Column(name = "content_quality_analysis", length = 2000, columnDefinition = "TEXT")
    private String contentQualityAnalysis;

    /**
     * 是否需要人工审核
     */
    @Column(name = "review_required")
    private Boolean reviewRequired;

    /**
     * 人工审核状态：PENDING（待审核）、APPROVED（已审核通过）、REJECTED（审核拒绝）、REVISED（已修订）
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "manual_review_status", length = 20)
    private SegmentationManualReviewStatus manualReviewStatus;

    /**
     * 人工评估员ID，关联sys_user表
     */
    @Column(name = "manual_evaluator_id")
    private Long manualEvaluatorId;

    /**
     * 人工评估时间
     */
    @Column(name = "manual_evaluation_time")
    private LocalDateTime manualEvaluationTime;

    /**
     * 人工评估耗时（毫秒）
     */
    @Column(name = "manual_evaluation_duration_ms")
    private Long manualEvaluationDurationMs;

    /**
     * 人工评估分数（0-100）
     */
    @Column(name = "manual_evaluation_score")
    private Double manualEvaluationScore;

    /**
     * 人工评估意见
     */
    @Column(name = "manual_evaluation_comment", length = 1000, columnDefinition = "TEXT")
    private String manualEvaluationComment;

    /**
     * 是否覆盖自动评估结果
     */
    @Column(name = "is_override_automatic")
    private Boolean isOverrideAutomatic;

    /**
     * 原始自动评估分数
     */
    @Column(name = "original_automatic_score")
    private Double originalAutomaticScore;

    /**
     * 评估基准版本
     */
    @Column(name = "evaluation_baseline_version", length = 20)
    private String evaluationBaselineVersion;

    /**
     * 质量标准参考ID
     */
    @Column(name = "quality_standard_reference_id")
    private Long qualityStandardReferenceId;

    /**
     * 与历史平均水平的偏差
     */
    @Column(name = "deviation_from_historical_average")
    private Double deviationFromHistoricalAverage;

    /**
     * 与同类任务平均水平的偏差
     */
    @Column(name = "deviation_from_similar_tasks_average")
    private Double deviationFromSimilarTasksAverage;

    /**
     * 质量趋势状态：IMPROVING（改善）、STABLE（稳定）、DECLINING（下降）、UNKNOWN（未知）
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "quality_trend_status", length = 20)
    private SegmentationQualityTrendStatus qualityTrendStatus;

    /**
     * 质量改进建议（JSON格式）
     */
    @Column(name = "quality_improvement_suggestions", length = 2000, columnDefinition = "TEXT")
    private String qualityImprovementSuggestions;

    /**
     * 评估置信度区间下限
     */
    @Column(name = "confidence_interval_lower")
    private Double confidenceIntervalLower;

    /**
     * 评估置信度区间上限
     */
    @Column(name = "confidence_interval_upper")
    private Double confidenceIntervalUpper;

    /**
     * 评估数据源版本
     */
    @Column(name = "evaluation_data_source_version", length = 50)
    private String evaluationDataSourceVersion;

    /**
     * 创建时间
     */
    @Column(name = "created_time", nullable = false)
    private LocalDateTime createdTime;

    /**
     * 更新时间
     */
    @Column(name = "updated_time")
    private LocalDateTime updatedTime;

    /**
     * 扩展属性（JSON格式）
     */
    @Column(name = "additional_attributes", length = 2000, columnDefinition = "TEXT")
    private String additionalAttributes;

    // ================================
    // JPA 关联关系
    // ================================

    /**
     * 关联的切分任务
     */
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "task_id", insertable = false, updatable = false)
    private SegmentationTask segmentationTask;

    // ===== 标准审计字段 =====

    /**
     * 创建时间（标准审计字段）
     */
    @CreationTimestamp
    @Column(name = "created_time_audit", nullable = false, updatable = false)
    private LocalDateTime createdTimeAudit;

    /**
     * 更新时间（标准审计字段）
     */
    @UpdateTimestamp
    @Column(name = "updated_time_audit", nullable = false)
    private LocalDateTime updatedTimeAudit;

    /**
     * 版本号（乐观锁）
     */
    @Version
    @Column(name = "version_audit")
    Long versionAudit = 0L;

    /**
     * 逻辑删除标记（标准审计字段）
     */
    @Column(name = "is_deleted_audit", nullable = false)
    Boolean isDeletedAudit = false;
}